<template>
  <div class="accountList">
    <div class="content">
      <div class="content-left">
        <div class="top">分类</div>
        <div class="classify-content">
          <div
            class="classify"
            v-for="(item, index) in forbiddenList"
            :key="index"
          >
            <div
              style="display: inline-block; width: 78%; cursor: pointer"
              :class="index == active ? 'active-style' : ''"
              @click="changeExposedDetail(index, item.id)"
            >
              {{ item.title }}
            </div>
            <div style="display: inline-block; vertical-align: middle">
              <span
                ><i
                  v-has="'packageReview:sensitiveWord:editType'"
                  style="margin-right: 5px; font-size: 18px; cursor: pointer"
                  class="el-icon-edit-outline"
                  @click="editClick(item, index)"
                ></i
              ></span>
              <span
                ><i
                      v-has="'packageReview:sensitiveWord:deleteType'"
                  style="font-size: 18px; cursor: pointer"
                  class="el-icon-delete"
                  @click="delClick(item)"
                ></i
              ></span>
            </div>
          </div>
          <div style="text-align: center">
            <el-button
              size="mini"
              @click="addExposedClick"
              type="success"
              plain
              icon="el-icon-plus"
              v-has="'packageReview:sensitiveWord:addType'"
              style="width: 100px"
              >新增</el-button
            >
          </div>
        </div>
      </div>
      <div class="content-right">
        <div class="topBtn">
          <el-button
            size="mini"
            @click="addExposedChildClick"
            type="success"
            plain
            icon="el-icon-plus"
            v-has="'packageReview:sensitiveWord:add'"
            >新增词汇</el-button
          >
        </div>
        <div class="searchBox">
          <el-form :model="searchForm" ref="searchFormRef">
            <el-row>
              <el-form-item prop="type">
                <div class="item">
                  <span v-html="'词汇'" class="nameSpan"></span>
                  <el-input
                    size="mini"
                    v-model="searchForm.content"
                    placeholder="请输入词汇"
                    maxlength="32"
                  ></el-input>
                </div>
              </el-form-item>
              <el-form-item>
                <el-button
                  @click="searchClick"
                  icon="el-icon-search"
                  type="primary"
                  size="mini"
                  >查询</el-button
                >
              </el-form-item>
            </el-row>
          </el-form>
        </div>
        <div class="tableBox" ref="tableBoxRef">
          <el-table
            v-loading="tableLoading"
            :header-cell-style="{
              background: '#F8F8F8',
              color: '#333',
              fontSize: '16px',
              fontWeight: '700',
            }"
            :height="screenHeight - 300"
            border
            :data="forbiddenChildData"
            style="width: 100%"
          >
            <el-table-column
              prop="title"
              align="center"
              label="类别"
               
            >
            </el-table-column>
            <el-table-column
              prop="content"
              align="center"
              label="禁用词汇"
               
            >
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  icon="el-icon-edit-outline"
                  size="mini"
                  type="text"
                  v-has="'packageReview:sensitiveWord:edit'"
                  @click="editClickChild(scope.row, scope.$index)"
                  >编辑</el-button
                >
                <el-button
                  icon="el-icon-delete"
                  size="mini"
                  type="text"
                  v-has="'packageReview:sensitiveWord:delete'"
                  @click="delClickChild(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
    <el-dialog :close-on-click-modal="false" 
      :title="title"
      :visible.sync="addEditDialogVisible"
      width="500px"
      @open="addEditDialogOpen"
      class="addEditAccountDialog"
    >
      <div class="content1">
        <el-form
          :model="addEditForm"
          :rules="addEditExposedRules"
          ref="addEditExposedFormRef"
        >
          <el-form-item prop="title" label="分类：">
            <div class="item">
              <el-input
                size="mini"
                v-model="addEditForm.title"
                placeholder="请输入分类"
              >
              </el-input>
            </div>
          </el-form-item>
          <el-form-item class="btns">
            <el-button size="mini" @click="addEditDialogVisible = false"
              >取 消</el-button
            >
            <el-button
              v-clickForbidden
              size="mini"
              type="primary"
              @click="addEditDialogSaveClick"
              >确 定</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" 
      :title="titleChild"
      :visible.sync="addEditDialogChildVisible"
      width="400px"
      @open="addEditDialogChildOpen"
      class="addEditAccountDialog"
    >
      <div class="content1">
        <el-form
          :model="addEditChildForm"
          :rules="addEditExposedChildRules"
          ref="addEditExposedChildFormRef"
        >
          <el-form-item prop="editChildId" label="分类：">
            <div class="item">
              <el-select filterable 
                :disabled="true"
                clearable
                size="mini"
                v-model="editChildId"
                placeholder="请选择分类："
              >
                <el-option
                  v-for="item in forbiddenList"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item prop="title" label="类别：">
            <div class="item">
              <el-input
                size="mini"
                v-model="addEditChildForm.title"
                placeholder="请输入类别"
                maxlength="32"
              >
              </el-input>
            </div>
          </el-form-item>

          <el-form-item prop="content" label="词汇：">
            <div class="item">
              <el-input
                type="textarea"
                size="mini"
                v-model="addEditChildForm.content"
                placeholder="如需添加多个，请用英文逗号隔开，最后一个词后面不可加符号，如：aaa,bbb,ccc,ddd"
                 :rows="6"
              >
              </el-input>
            </div>
          </el-form-item>
          <el-form-item class="btns">
            <el-button size="mini" @click="addEditDialogChildVisible = false"
              >取 消</el-button
            >
            <el-button
              v-clickForbidden
              size="mini"
              type="primary"
              @click="addEditDialogChildSaveClick"
              >确 定</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      active: 0,
      editChildId: "",
      forbiddenChildData: [],
      typeOptions: [
        { id: 3, name: "清洁类" },

        { id: 4, name: "皮肤护理" },

        { id: 5, name: "彩妆" },

        { id: 6, name: "发型" },
      ],

      typeSetOptions: [
        { id: 1, name: "淋洗类" },
        { id: 2, name: "驻留类" },
      ],
      searchForm: {
        content: "",
      },
      tableLoading: false,
      screenHeight: document.documentElement.clientHeight,
      forbiddenChildData: [],
      editId: "",
      addEditForm: {
        title: "",
      },
      title: "新增分类",
      titleChild: "新增词汇",
      forbiddenList: [],
      addEditDialogVisible: false,
      addEditChildForm: {},
      addEditExposedRules: {
        name: [{ required: true, message: "请输入词汇名称", trigger: "blur" }],
      },
      addEditExposedChildRules: {
        title: [
          { required: true, message: "请输入类别", trigger: "blur" },
        ],
        content: [
          { required: true, message: "请输入词汇", trigger: "blur" },
        ],
      },
      addEditDialogChildVisible: false,
    };
  },
  methods: {
    delClickChild(row) {
      this.$confirm("确定删除当前词汇吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
         
          this.$api.delForbiddenChild(row.id).then((res) => {
            if (res.data.code == "00000") {
              this.$message.success("删除成功");
              this.getForbiddenChildList(row.kindId);
            }
          });
        })
        .catch(() => {});
    },
    /**
     * 编辑按钮
     * @param {*} row
     */
    async editClickChild(row, index) {

      this.titleChild = "编辑词汇";
      this.addEditDialogChildVisible = true;
      this.editChildId = row.kindId;
      this.$nextTick(() => {
        this.addEditChildForm = JSON.parse(
          JSON.stringify(this.forbiddenChildData[index])
        );
      });
    },

    searchClick() {

      this.pageNum = 1;
      this.getForbiddenChildList(this.forbiddenList[this.active].id);
    },
    addExposedChildClick() {
        this.editChildId = this.forbiddenList[this.active].id
      this.titleChild = "新增词汇";
      this.addEditDialogChildVisible = true;
    },
    /**
     * 新增编辑保存按钮
     * @param {*}
     */
    addEditDialogSaveClick() {
      this.$refs.addEditExposedFormRef.validate(async (boolean) => {
        if (boolean) {
         let data = {
            id: this.editChildId,
            ...this.addEditForm,
          };
          if (this.title == "新增分类") {
            data.id = "";
            const res = await this.$api.addForbiddenClassify(data);
            if (res.data.code == "00000") {
              this.$message.success("新增成功");
              this.getForbiddenList();
              this.addEditDialogVisible = false;
            }
          } else {
            console.log(data);
            const res = await this.$api.editForbiddenClassify(data);
            if (res.data.code == "00000") {
              this.$message.success("修改成功");
              this.getForbiddenList();
              this.addEditDialogVisible = false;
            }
          }
        }
      });
    },
    addEditDialogChildSaveClick() {
      this.$refs.addEditExposedChildFormRef.validate(async (boolean) => {
        if (boolean) {
          
          if (this.titleChild == "新增词汇") {
            let data = {
            kindId: this.editChildId,
            ...this.addEditChildForm,
          };
            const res = await this.$api.addForbiddenChild(data);
            if (res.data.code == "00000") {
              this.$message.success("新增成功");
              this.getForbiddenChildList(this.editChildId);
              this.addEditDialogChildVisible = false;
            }
          } else {
            let data = {
            id: this.editChildId,
            ...this.addEditChildForm,
          };
            const res = await this.$api.editForbiddenChild(data);
            if (res.data.code == "00000") {
              this.$message.success("修改成功");
              console.log(this.addEditChildForm);
              this.getForbiddenChildList(this.addEditChildForm.kindId);
              this.addEditDialogChildVisible = false;
            }
          }
        }
      });
    },
    /**
     * 删除按钮
     * @param {*} row
     */
    delClick(row) {
      this.$confirm("确定删除当前分类吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log(row.id);
          this.$api.delForbiddenClassify(row.id).then((res) => {
            if (res.data.code == "00000") {
              this.$message.success("删除成功");
              this.getForbiddenList();
            }
          });
        })
        .catch(() => {});
    },
    /**
     * 编辑按钮
     * @param {*} row
     */
    async editClick(row, index) {
      this.title = "编辑词汇";
      this.addEditDialogVisible = true;
      this.editId = row.id;
      this.$nextTick(() => {
        this.addEditForm = JSON.parse(JSON.stringify(this.forbiddenList[index]));
      });
    },
    /**
     * 新增字典
     * @param {*}
     */
    addExposedClick() {
      this.title = "新增分类";
      this.addEditDialogVisible = true;
    },
    addEditDialogOpen() {
      if (this.$refs.addEditExposedFormRef)
        this.$refs.addEditExposedFormRef.resetFields();
    },
    addEditDialogChildOpen() {
      if (this.$refs.addEditExposedChildFormRef)
        this.$refs.addEditExposedChildFormRef.resetFields();
    },
    async getForbiddenList() {
      const res = await this.$api.forbiddenClassifyList();
      this.forbiddenList = res.data.result;
      this.getForbiddenChildList(this.forbiddenList[0].id);
    },
    changeExposedDetail(index, id) {
      this.searchForm.content=''
      this.active = index;
      this.getForbiddenChildList(id);
    },
    async getForbiddenChildList(id) {
      let data = {
        kindId: id,
        ...this.searchForm,
      };
      const res = await this.$api.forbiddenChildList(data);
      this.forbiddenChildData = res.data.result;
    },
  },
  created() {
    this.getForbiddenList();
  },
};
</script>
<style  lang='less' scoped>
.accountList {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  .active-style {
    color: #409eff;
  }
  .addEditAccountDialog {
    .el-form-item {
      .el-select {
        width: 100%;
      }
    }
    .btns {
      text-align: right;
      margin-bottom: 0;
    }
  }
  .topBtn {
    text-align: right;
    margin: 10px 0;
  }
  .btns {
    text-align: right;
    margin-bottom: 0;
  }
  .content {
    display: flex;
    .content-left {
      width: 260px;

      border: 1px solid #c1c4cc;
      .top {
        text-align: center;
        font-size: 16px;
        padding: 10px 0;
        background-color: #efefef;
        border-bottom: 1px solid #c1c4cc;
      }
      .classify-content {
        font-size: 14px;
        padding: 15px;
        .classify {
          margin-bottom: 15px;
        }
      }
    }
    .content-right {
      width: 60%;
      margin-left: 40px;
      .searchBox {
        .el-row {
          display: flex;
          flex-wrap: wrap;
          .el-form-item {
            margin-bottom: 0;
          }
          .item {
            display: flex;
            align-items: center;
            margin-right: 20px;
            .nameSpan {
              padding-right: 10px;
              box-sizing: border-box;
              white-space: nowrap;
            }
            .el-input {
              width: 162px;
            }
          }
        }
      }
      .tableBox {
        flex: 1;
        .active {
          color: #409eff;
          cursor: pointer;
          text-decoration: underline;
        }
        .bottomBtn {
          height: 50px;
          display: flex;
          justify-content: flex-end;
          align-items: center;
        }
      }
    }
  }
}
/deep/.el-button--success.is-plain {
  color: #fff;
  border-color: #409eff;
  background-color: #409eff;
}

/deep/.el-form-item {
  margin-bottom: 12px;
}

</style>
